package com.lsfb.signln.Leave.PersonnelSelect;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.URLString;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/2/3 0003.
 */

public class PersonnelSelectAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private LayoutInflater inflater;
    private List<SelfDeparListBean.ListBean> groups = new ArrayList<>();
    private CheckListener listener;

    public interface CheckListener {
        void onCheckAll(int groupsPosition);

        void onChild(SelfDeparListBean.ListBean.PlistBean model, int groupsPosition);
    }

    public PersonnelSelectAdapter(Context mContext, List<SelfDeparListBean.ListBean> groups, CheckListener listener) {
        this.mContext = mContext;
        this.listener = listener;
        this.groups = groups;
        inflater = LayoutInflater.from(mContext);
    }

    //返回一级列表的个数
    @Override
    public int getGroupCount() {
        return groups.size();
    }

    //返回每个二级列表的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        if (groups != null) {
            return groups.get(groupPosition).getPlist().size();
        }
        return 0;
    }

    //返回一级列表的单个item（返回的是对象）
    @Override
    public Object getGroup(int groupPosition) {
        if (groups.size() == 0) {
            return "";
        }
        return groups.get(groupPosition);
    }

    //返回二级列表中的单个item（返回的是对象）
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (groups.get(groupPosition).getPlist().size() == 0) {
            return "";
        }
        return groups.get(groupPosition).getPlist().get(childPosition);
    }

    //一级列表ID
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //二级列表ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //每个item的id是否是固定？一般为true
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //【重要】填充一级列表
    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hyx_group, null);
            holder = new GroupHolder();
            holder.tv_department = (TextView) convertView.findViewById(R.id.tv_department);
            holder.item_main = (RelativeLayout) convertView.findViewById(R.id.item_main);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            holder.ll_group = (LinearLayout) convertView.findViewById(R.id.ll_group);
            holder.checkbox_group = (CheckBox) convertView.findViewById(R.id.checkbox_group);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.tv_department.setText(groups.get(groupPosition).getDname());
        holder.checkBox.setChecked(isExpanded);
        holder.checkbox_group.setChecked(groups.get(groupPosition).isCheckAll);

        holder.ll_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCheckAll(groupPosition);
                }
            }
        });
        return convertView;
    }

    //【重要】填充二级列表
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_hyx_child, null);
            holder = new ChildHolder();
            holder.iv_head = (SimpleDraweeView) convertView.findViewById(R.id.iv_head);
            holder.iv_name = (TextView) convertView.findViewById(R.id.item_text_head);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.checkbox_child = (CheckBox) convertView.findViewById(R.id.checkbox_child);
            holder.item_child_main = (RelativeLayout) convertView.findViewById(R.id.item_child_main);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        if (groups.get(groupPosition).getPlist().get(childPosition).getHeadimg() != null &&
                !groups.get(groupPosition).getPlist().get(childPosition).getHeadimg().isEmpty()) {
            holder.iv_head.setVisibility(View.VISIBLE);
            holder.iv_name.setVisibility(View.GONE);
            holder.iv_head.setImageURI(URLString.IMAGES + groups.get(groupPosition).getPlist().get(childPosition).getHeadimg());
        } else {
            holder.iv_head.setVisibility(View.GONE);
            holder.iv_name.setVisibility(View.VISIBLE);
            holder.iv_name.setText(groups.get(groupPosition).getPlist().get(childPosition).getName()
                    .substring(groups.get(groupPosition).getPlist().get(childPosition).getName().length() - 2,
                            groups.get(groupPosition).getPlist().get(childPosition).getName().length()));
        }
        holder.tv_name.setText(groups.get(groupPosition).getPlist().get(childPosition).getName());
        holder.item_child_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onChild(groups.get(groupPosition).getPlist().get(childPosition), groupPosition);
                }
            }
        });

        holder.checkbox_child.setChecked(groups.get(groupPosition).getPlist().get(childPosition).isCheck);
        return convertView;
    }

    //二级列表中的item是否能够被选中？可以改为true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupHolder {
        private TextView tv_department;
        private RelativeLayout item_main;
        private CheckBox checkBox;
        private CheckBox checkbox_group;
        private LinearLayout ll_group;
    }

    class ChildHolder {
        private SimpleDraweeView iv_head;
        private TextView tv_name;
        private CheckBox checkbox_child;
        private RelativeLayout item_child_main;
        private TextView iv_name;
    }
}
